package com.ruoyi.web.controller.client.consumer;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.interceptor.LoginUser;
import com.ruoyi.framework.interceptor.MpAuthorize;
import com.ruoyi.system.service.ISysConsumerService;
import com.ruoyi.web.dto.consumer.*;
import com.ruoyi.web.service.consumer.MpConsumerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Api(tags = "用户端登录/注册")
@RestController
@RequestMapping("/client/consumer")
public class SysConsumerController {

    @Autowired
    private MpConsumerService sysConsumerService;


    @ApiOperation(value = "手机号登陆", notes = "手机号登录需要注册", response = LoginInfo.class)
    @PostMapping("login_by_phone")
    public AjaxResult loginByPhone(@RequestBody @Valid AppLoginRequest appLoginRequest, HttpServletRequest request) {
        LoginInfo loginInfo = sysConsumerService.login(appLoginRequest);
        return AjaxResult.success(loginInfo);
    }


    @ApiOperation(value = "注册", notes = "注册需要上传身份证号", response = LoginInfo.class)
    @PostMapping("register")
    public AjaxResult register(@RequestBody @Valid AppRegisterRequest registerRequest, HttpServletRequest request) {
        LoginInfo loginInfo = sysConsumerService.register(registerRequest);
        return AjaxResult.success(loginInfo);
    }

    @ApiOperation(value = "发送验证码", notes = "安卓H5发送验证码", response = Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true),
    })
    @PostMapping("sendAuthCode")
    public AjaxResult sendAuthCode(@RequestBody SendAuthCode sendAuthCode, HttpServletRequest request) throws Exception {
        return AjaxResult.success(sysConsumerService.sendAuthCode(sendAuthCode, request));
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", response = LoginInfo.class)
    @MpAuthorize
    @GetMapping("detail")
    public AjaxResult detail(@LoginUser Long userId) {
        return AjaxResult.success(sysConsumerService.getUserInfo(userId));
    }

    @ApiOperation(value = "修改密码", notes = "用户修改密码", response = Boolean.class)
    @MpAuthorize
    @PostMapping("updatePwd")
    public AjaxResult updatePwd(@RequestBody UpdatePassword updatePassword,
                                @LoginUser Long userId) throws Exception {
        return AjaxResult.success(sysConsumerService.updatePwd(updatePassword, userId));
    }


}
